package com.example.yiwei.crm.view.activity.menuActivity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.view.activity.healthActivity.HealthSimpleClueMnagerActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;
import com.example.yiwei.crm.view.frament.menuFragment.personal_information_fragment;
import com.example.yiwei.crm.view.frament.menuFragment.home_fragment;
import com.example.yiwei.crm.view.frament.menuFragment.office_fragment;
import com.example.yiwei.crm.view.frament.menuFragment.message_fragment;
import com.example.yiwei.crm.view.frament.menuFragment.crm_fragment;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;


//菜单页面
public class HomeActivity extends FragmentActivity {
    //悬浮按钮
    ImageView fabIconStar1;
    FloatingActionMenu leftCenterMenu;
    FloatingActionMenu leftCenterMenu1;
    FloatingActionButton leftCenterButton;
    FloatingActionButton leftCenterButton1;
    TextView tv1,tv2,tv3,tv4,tv5;
    ImageView lcIcon1;
    TextView tv[] = {tv1,tv2,tv3,tv4,tv5};
    private FragmentTabHost fragmentTabHost;
    //定义一个布局
    private LayoutInflater layoutInflater;
    private String texts[] = { "工作台", "消息", "CRM", "办公", "我" };
    private int imageButton[] = {R.drawable.bt_work_selectoe,R.drawable.bt_message_selector,R.drawable.bt_crm_selector,
    R.drawable.bt_work_selector,R.drawable.bt_personal_selector};
    private Class framentArray[]={home_fragment.class, message_fragment.class, crm_fragment.class,
            office_fragment.class,personal_information_fragment.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //添加悬浮按钮
        addXuanFu1();
        addXuanFu2();

        leftCenterMenu1.setStateChangeListener(new FloatingActionMenu.MenuStateChangeListener() {
            @Override
            public void onMenuOpened(FloatingActionMenu floatingActionMenu) {
                leftCenterMenu.open(true);
                // 增加按钮中的+号图标顺时针旋转45度
//                // Rotate the icon of rightLowerButton 45 degrees clockwise
//                icon.setRotation(0);
//                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 45);
//                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(icon, pvhR);
//                animation.start();
                fabIconStar1.setRotation(0);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION,45);
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(fabIconStar1,pvhR);
                animator.start();

            }

            @Override
            public void onMenuClosed(FloatingActionMenu floatingActionMenu) {
                leftCenterMenu.close(true);
                fabIconStar1.setRotation(45);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION,0);
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(fabIconStar1,pvhR);
                animator.start();
            }
        });
        layoutInflater = LayoutInflater.from(this);
        fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this,getSupportFragmentManager(),R.id.maincontent);
        for (int i = 0; i < texts.length; i++) {
            TabHost.TabSpec spec = fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
            fragmentTabHost.addTab(spec, framentArray[i], null);

            //设置背景(必须在addTab之后，由于需要子节点（底部菜单按钮）否则会出现空指针异常)
            fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
        }
        tv[0].setTextColor(Color.parseColor("#55ACEE"));
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                if(s.equals(texts[0])){
                    leftCenterButton.setVisibility(View.VISIBLE);
                    leftCenterButton1.setVisibility(View.VISIBLE);
                }else {
                    leftCenterButton.setVisibility(View.INVISIBLE);
                    leftCenterButton1.setVisibility(View.INVISIBLE);
                    if (leftCenterMenu1.isOpen()) {
                        leftCenterMenu1.close(true);
                    }
                    if (leftCenterMenu.isOpen()) {
                        leftCenterMenu.close(true);
                    }
                }

                for (int i = 0; i < texts.length; i++) {
                    if (s.equals(texts[i])) {
                        tv[i].setTextColor(Color.parseColor("#55ACEE"));
                    } else {
                        tv[i].setTextColor(Color.parseColor("#A7A7A7"));
                    }
                }


            }
        });
        lcIcon1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HomeActivity.this, HealthSimpleClueMnagerActivity.class);
                startActivity(intent);
            }
        });
        if ("back".equals(getIntent().getStringExtra("tag"))){
            fragmentTabHost.setCurrentTab(2);
        }
    }
   private View getView(int i){
       View view = layoutInflater.inflate(R.layout.layout_item,null);
       ImageView im = (ImageView) view.findViewById(R.id.item_image);
       im.setImageResource(imageButton[i]);
        tv[i] = (TextView) view.findViewById(R.id.item_textview);
       tv[i].setText(texts[i]);
       TextView tv2 = (TextView) view.findViewById(R.id.tab_indicator_unread);
       if (i==1){
          tv2.setText("2");
       }else if(i==3){

       }else {
           tv2.setVisibility(View.GONE);
       }

       return view;
   }
    public void  addXuanFu1(){
        /**
         * 一个FloatingActionButton有一个FloatingActionMenu，FloatingActionMenu添加动画弹出的子菜单。
         * FloatingActionButton的按钮点击事件将触发、弹出FloatingActionMenu中包含的子菜单。
         * FloatingActionMenu使用attachTo方法附着在一个FloatingActionButton上。
         * FloatingActionMenu在attachTo到一个FloatingActionButton后，两者之间发生关联。
         * FloatingActionMenu在添加子菜单时候，首先需要一个SubActionButton.Builder，该SubActionButton.
         * Builder通过setContentView(ImageView
         * image).build()把一个ImageView创建生产一个SubActionButton ,
         * 然后通过FloatingActionMenu.Builder的add方法把前面生成的SubActionButton添加进去。
         *
         *
         * 按钮的旋转动画在onMenuOpened和onMenuClosed中做。
         *
         */
        int redActionButtonSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_size);
        int redActionButtonMargin = getResources().getDimensionPixelOffset(R.dimen.red_action_button_margin);
        int redActionButtonMarginRight = getResources().getDimensionPixelOffset(R.dimen.action_button_margin);
        int redActionButtonContentSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_size);
        int redActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.red_action_button_content_margin);
        int redActionMenuRadius = getResources().getDimensionPixelSize(R.dimen.red_action_menu_radius);
        int blueSubActionButtonSize = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_size);
        int blueSubActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.blue_sub_action_button_content_margin);

        final ImageView fabIconStar = new ImageView(this);
        fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_important));

        FloatingActionButton.LayoutParams starParams = new FloatingActionButton.LayoutParams(redActionButtonSize,
                redActionButtonSize);
        starParams.setMargins(redActionButtonMargin, redActionButtonMargin, redActionButtonMarginRight,
                redActionButtonMargin);
        fabIconStar.setLayoutParams(starParams);

        FloatingActionButton.LayoutParams fabIconStarParams = new FloatingActionButton.LayoutParams(
                redActionButtonContentSize, redActionButtonContentSize);
        fabIconStarParams.setMargins(redActionButtonContentMargin, redActionButtonContentMargin,
                redActionButtonContentMargin, redActionButtonContentMargin);

        leftCenterButton = new FloatingActionButton.Builder(this)
                .setContentView(fabIconStar, fabIconStarParams)
//                .setBackgroundDrawable(R.drawable.button_action_red_selector)// 红底白星按钮选中时候状态变化和背景颜色
                .setPosition(FloatingActionButton.POSITION_BOTTOM_RIGHT)// 左边居中
                .setLayoutParams(starParams).build();

        // Set up customized SubActionButtons for the right center menu
        SubActionButton.Builder lCSubBuilder = new SubActionButton.Builder(this);
        lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_action_blue_selector));

        FrameLayout.LayoutParams blueContentParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        blueContentParams.setMargins(blueSubActionButtonContentMargin, blueSubActionButtonContentMargin,
                blueSubActionButtonContentMargin, blueSubActionButtonContentMargin);
        lCSubBuilder.setLayoutParams(blueContentParams);
        // Set custom layout params
        FrameLayout.LayoutParams blueParams = new FrameLayout.LayoutParams(blueSubActionButtonSize,
                blueSubActionButtonSize);
        lCSubBuilder.setLayoutParams(blueParams);

        ImageView lcIcon11 = new ImageView(this);
        ImageView lcIcon2 = new ImageView(this);
        ImageView lcIcon3 = new ImageView(this);

        lcIcon11.setImageDrawable(getResources().getDrawable(R.drawable.u116));
        lcIcon2.setImageDrawable(getResources().getDrawable(R.drawable.u117));
        lcIcon3.setImageDrawable(getResources().getDrawable(R.drawable.u118));

        // Build another menu with custom options
        leftCenterMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(lCSubBuilder.setContentView(lcIcon11, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon2, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon3, blueContentParams).build())
                .setRadius(redActionMenuRadius).setStartAngle(180).setEndAngle(270).attachTo(leftCenterButton).build();
    }
    public void  addXuanFu2(){
        int redActionButtonSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_size);
        int redActionButtonMargin = getResources().getDimensionPixelOffset(R.dimen.red_action_button_margin);
        int redActionButtonMarginRight = getResources().getDimensionPixelOffset(R.dimen.action_button_margin);
        int redActionButtonContentSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_size);
        int redActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.red_action_button_content_margin);
        int redActionMenuRadius = getResources().getDimensionPixelSize(R.dimen.red_action_menu_radius1);
        int blueSubActionButtonSize = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_size);
        int blueSubActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.blue_sub_action_button_content_margin);

        fabIconStar1 = new ImageView(this);
        fabIconStar1.setImageDrawable(getResources().getDrawable(R.drawable.add_xuanfu));

        FloatingActionButton.LayoutParams starParams = new FloatingActionButton.LayoutParams(redActionButtonSize,
                redActionButtonSize);
        starParams.setMargins(redActionButtonMargin, redActionButtonMargin, redActionButtonMarginRight,
                redActionButtonMargin);
        fabIconStar1.setLayoutParams(starParams);

        FloatingActionButton.LayoutParams fabIconStarParams = new FloatingActionButton.LayoutParams(
                redActionButtonContentSize, redActionButtonContentSize);
        fabIconStarParams.setMargins(redActionButtonContentMargin, redActionButtonContentMargin,
                redActionButtonContentMargin, redActionButtonContentMargin);

        leftCenterButton1 = new FloatingActionButton.Builder(this)
                .setContentView(fabIconStar1, fabIconStarParams)
                .setBackgroundDrawable(R.drawable.button_action_red_selector)// 红底白星按钮选中时候状态变化和背景颜色
                .setPosition(FloatingActionButton.POSITION_BOTTOM_RIGHT)// 右边居中
                .setLayoutParams(starParams).build();

        // Set up customized SubActionButtons for the right center menu
        SubActionButton.Builder lCSubBuilder = new SubActionButton.Builder(this);
        lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_action_blue_selector));

        FrameLayout.LayoutParams blueContentParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        blueContentParams.setMargins(blueSubActionButtonContentMargin, blueSubActionButtonContentMargin,
                blueSubActionButtonContentMargin, blueSubActionButtonContentMargin);
        lCSubBuilder.setLayoutParams(blueContentParams);
        // Set custom layout params
        FrameLayout.LayoutParams blueParams = new FrameLayout.LayoutParams(blueSubActionButtonSize,
                blueSubActionButtonSize);
        lCSubBuilder.setLayoutParams(blueParams);

        lcIcon1 = new ImageView(this);
        ImageView lcIcon2 = new ImageView(this);
        ImageView lcIcon3 = new ImageView(this);
        ImageView lcIcon4 = new ImageView(this);
        ImageView lcIcon5 = new ImageView(this);

        lcIcon1.setImageDrawable(getResources().getDrawable(R.drawable.ks));
        lcIcon2.setImageDrawable(getResources().getDrawable(R.drawable.u120));
        lcIcon3.setImageDrawable(getResources().getDrawable(R.drawable.u115));
        lcIcon4.setImageDrawable(getResources().getDrawable(R.drawable.u121));
        lcIcon5.setImageDrawable(getResources().getDrawable(R.drawable.u122));

        // Build another menu with custom options
        leftCenterMenu1 = new FloatingActionMenu.Builder(this)
                .addSubActionView(lCSubBuilder.setContentView(lcIcon1, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon2, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon3, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon4, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon5, blueContentParams).build())
                .setRadius(redActionMenuRadius).setStartAngle(180).setEndAngle(270).attachTo(leftCenterButton1).build();

    }
}
